package com.globalresources.dao.hibernate;

import java.util.List;

import sun.print.resources.serviceui;

import com.globalresources.commons.Constant;
import com.globalresources.dao.inface.IMessageReceiverDao;
import com.globalresources.po.Message;
import com.globalresources.po.MessageReceiver;
import com.trg.dao.dao.standard.GenericDAOImpl;
import com.trg.search.Filter;
import com.trg.search.Search;

/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since 创建时间：Aug 7, 2009 3:36:53 PM
 */
public class MessageReceiverDaoHibernate extends GenericDAOImpl<MessageReceiver, Integer> implements IMessageReceiverDao {
	
	/**
	 * <p>查询消息接受者
	*@author aaron.lu(luzhiyuan)
	*@param username
	*@param isReceive
	*@param isRead
	*@param isDelete
	*@param isDeleteMessage
	*@param pageSize
	*@param startRow
	*@return
	 */
	public List<MessageReceiver> getMessagerReceivers(String username, Byte isReceive, Byte isRead, Byte isDelete, Byte isDeleteMessage, int pageSize, int startRow){
		Search search = new Search();
		search.setFirstResult(startRow);
		search.setMaxResults(startRow + pageSize);
		search.addFilterEqual("user.username", username);
		search.addFilterEqual("isReceive", isReceive);
		search.addFilterEqual("isRead", isRead);
		search.addFilterEqual("isDelete", isDelete);
		search.addFilterEqual("message.isDelete", isDeleteMessage);
		search.addSort("sendTime", true);
		return super.search(search);
	}
	
	/**
	 * <p>查询消息接受者总纪录数
	*@author aaron.lu(luzhiyuan)
	*@param username
	*@param isReceive
	*@param isRead
	*@param isDelete
	*@param isDeleteMessage
	*@return
	 */
	public int countMessageReceiver(String username, Byte isReceive, Byte isRead, Byte isDelete, Byte isDeleteMessage){
		Search search = new Search();
		search.addFilterEqual("user.username", username);
		search.addFilterEqual("isReceive", isReceive);
		search.addFilterEqual("isRead", isRead);
		search.addFilterEqual("isDelete", isDelete);
		search.addFilterEqual("message.isDelete", isDeleteMessage);
		return super.count(search);
	}
	
	/**
	 * <p>更新实体
	*@author songduanlei@gmail.com
	*@return
	 */
	public void saveOrUpdateMessageReceiver(MessageReceiver messageReceiver){
		super._saveOrUpdate(messageReceiver);
	}

	/**
	 * <p>查询消息接受者
	*@author liming.yu(yulm88@gmail.com)
	*@param keyWord  在Message表的tile和content中匹配的字符串
	*@param username 
	*@param isReceive
	*@param isRead
	*@param isDelete
	*@param isDeleteMessage
	*@param pageSize
	*@param startRow
	*@return
	 */
	public List<MessageReceiver> getMatchingMessagerReceivers(String keyWord,String username, Byte isReceive, Byte isRead, Byte isDelete, Byte isDeleteMessage, int pageSize, int startRow){
		Search search = new Search();
		search.setFirstResult(startRow);
		search.setMaxResults(startRow + pageSize);
		search.addFilterEqual("user.username", username);
		search.addFilterEqual("isReceive", isReceive);
		search.addFilterEqual("isRead", isRead);
		search.addFilterEqual("isDelete", isDelete);
		search.addFilterEqual("message.isDelete", isDeleteMessage);
		search.addSort("sendTime", true);
		search.addFilterOr(Filter.like("message.title", "%" + keyWord + "%"),
				Filter.like("message.content", "%" + keyWord + "%")		
		);
		return super.search(search);
	}
	
	/**
	 * <p>查询消息接受者总纪录数
	*@author liming.yu(yulm88@gmail.com)
	*@param username  邮件接收者
	*@param isReceive
	*@param isRead
	*@param isDelete
	*@param isDeleteMessage
	*@return
	 */
	public int countMatchingMessageReceiver(String keyWord,String username, Byte isReceive, Byte isRead, Byte isDelete, Byte isDeleteMessage){
		Search search = new Search();
		search.addFilterEqual("user.username", username);
		search.addFilterEqual("isReceive", isReceive);
		search.addFilterEqual("isRead", isRead);
		search.addFilterEqual("isDelete", isDelete);
		search.addFilterEqual("message.isDelete", isDeleteMessage);
		search.addFilterOr(Filter.like("message.title", "%" + keyWord + "%"),
				Filter.like("message.content", "%" + keyWord + "%")		
		);
		return super.count(search);
	}
	
}
